package com.axdoc.wallet.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.axdoc.wallet.dao.IPlatformWalletDao;
import com.axdoc.wallet.entity.PlatformWallet;
import com.axdoc.wallet.entity.PlatformWalletRecords;

@Component("platformWalletDao")
public class PlatformWalletDaoImpl implements IPlatformWalletDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean addPlatformRecords(PlatformWalletRecords records) {
		boolean result = false;
		/* 插入实现 */
		int i = jdbcTemplate.update(
				"insert into platform_wallet_records(wallet_no,trade_no,trx_no,biz_no,change_amount,change_direction,type,coupon_id,vip_service_id,activity_id,trx_code,trx_desc,biz_code,biz_desc) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { records.getWalletNo(), records.getTradeNo(), records.getTrxNo(), records.getBizNo(),
						records.getChangeAmount(), records.getChangeDirection(), records.getType(),
						records.getCouponId(), records.getVipServiceId(), records.getActivityId(), records.getTrxCode(),
						records.getTrxDesc(), records.getBizCode(), records.getBizDesc() });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean updateBalanceById(int id, int balance) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update("update platform_wallet set balance=balance+? where id=?",
				new Object[] { balance, id });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PlatformWallet queryIdByWalletNo(String walletNo) {
		List<PlatformWallet> list = new ArrayList<PlatformWallet>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from platform_wallet  where wallet_no = ? ");
		list = (List<PlatformWallet>) jdbcTemplate.query(sql.toString(), new Object[] { walletNo }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				PlatformWallet pw = new PlatformWallet();
				pw.setId(rs.getInt("id"));
				pw.setWalletNo(rs.getString("wallet_no"));
				pw.setBalance(rs.getInt("balance"));
				pw.setCreateTime(rs.getTimestamp("create_time"));
				pw.setUpdateTime(rs.getTimestamp("update_time"));
				return pw;
			}
		});
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PlatformWalletRecords> queryRecordsByTradeNo(String tradeNo) {
		List<PlatformWalletRecords> list = new ArrayList<PlatformWalletRecords>();
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select * from platform_wallet_records where trade_no = ? ");
		list = (List<PlatformWalletRecords>) jdbcTemplate.query(sql.toString(), new Object[] { tradeNo }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				PlatformWalletRecords record = new PlatformWalletRecords();
				record.setId(rs.getInt("id"));
				record.setWalletNo(rs.getString("wallet_no"));
				record.setTradeNo(rs.getString("trade_no"));
				record.setTrxNo(rs.getString("trx_no"));
				record.setBizNo(rs.getString("biz_no"));
				record.setChangeAmount(rs.getInt("change_amount"));
				record.setChangeDirection(rs.getInt("change_direction"));
				record.setType(rs.getInt("type"));
				record.setCouponId(rs.getString("coupon_id"));
				record.setVipServiceId(rs.getString("vip_service_id"));
				record.setActivityId(rs.getString("activity_id"));
				record.setTrxCode(rs.getString("trx_code"));
				record.setTrxDesc(rs.getString("trx_desc"));
				record.setBizCode(rs.getString("biz_code"));
				record.setBizDesc(rs.getString("biz_desc"));
				record.setCreateTime(rs.getTimestamp("create_time"));
				record.setUpdateTime(rs.getTimestamp("update_time"));
				return record;
			}
		});
		return list;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PlatformWalletRecords> queryRecordsByBizNo(String bizNo) {
		List<PlatformWalletRecords> list = new ArrayList<PlatformWalletRecords>();
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select * from platform_wallet_records where biz_no = ? ");
		list = (List<PlatformWalletRecords>) jdbcTemplate.query(sql.toString(), new Object[] { bizNo }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				PlatformWalletRecords record = new PlatformWalletRecords();
				record.setId(rs.getInt("id"));
				record.setWalletNo(rs.getString("wallet_no"));
				record.setTradeNo(rs.getString("trade_no"));
				record.setTrxNo(rs.getString("trx_no"));
				record.setBizNo(rs.getString("biz_no"));
				record.setChangeAmount(rs.getInt("change_amount"));
				record.setChangeDirection(rs.getInt("change_direction"));
				record.setType(rs.getInt("type"));
				record.setCouponId(rs.getString("coupon_id"));
				record.setVipServiceId(rs.getString("vip_service_id"));
				record.setActivityId(rs.getString("activity_id"));
				record.setTrxCode(rs.getString("trx_code"));
				record.setTrxDesc(rs.getString("trx_desc"));
				record.setBizCode(rs.getString("biz_code"));
				record.setBizDesc(rs.getString("biz_desc"));
				record.setCreateTime(rs.getTimestamp("create_time"));
				record.setUpdateTime(rs.getTimestamp("update_time"));
				return record;
			}
		});
		return list;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PlatformWalletRecords> queryRecordsByTime(Timestamp startTime, Timestamp endTime) {
		List<PlatformWalletRecords> list = new ArrayList<PlatformWalletRecords>();
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select * from platform_wallet_records where create_time >= ? and create_time<? ");
		list = (List<PlatformWalletRecords>) jdbcTemplate.query(sql.toString(), new Object[] { startTime,endTime }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				PlatformWalletRecords record = new PlatformWalletRecords();
				record.setId(rs.getInt("id"));
				record.setWalletNo(rs.getString("wallet_no"));
				record.setTradeNo(rs.getString("trade_no"));
				record.setTrxNo(rs.getString("trx_no"));
				record.setBizNo(rs.getString("biz_no"));
				record.setChangeAmount(rs.getInt("change_amount"));
				record.setChangeDirection(rs.getInt("change_direction"));
				record.setType(rs.getInt("type"));
				record.setCouponId(rs.getString("coupon_id"));
				record.setVipServiceId(rs.getString("vip_service_id"));
				record.setActivityId(rs.getString("activity_id"));
				record.setTrxCode(rs.getString("trx_code"));
				record.setTrxDesc(rs.getString("trx_desc"));
				record.setBizCode(rs.getString("biz_code"));
				record.setBizDesc(rs.getString("biz_desc"));
				record.setCreateTime(rs.getTimestamp("create_time"));
				record.setUpdateTime(rs.getTimestamp("update_time"));
				return record;
			}
		});
		return list;
	}
	
	

}
